import { _decorator, Component, Collider2D, Contact2DType,find, IPhysics2DContact, CircleCollider2D, Vec3, Prefab, instantiate,Node } from 'cc';
const { ccclass, property } = _decorator;
import { PlayerController } from './PlayerController';
@ccclass('Fireball')
export class Fireball extends Component {
    // 玩家属性节点，用于读取各种属性
    @property(Node)
    private Player:Node = null;
    
    private isAttacking: boolean = false;
    private collider: CircleCollider2D = null;

    // 武器旋转定时器
    public FireballRunTimer = null;
    // 旋转角度
    private rotate = 3.6
    // 武器攻击力
    private damage = 50
    // 武器转动速度
    private rotateSpeed = 50
    // 武器特殊攻击
    public ex = '0'
    onLoad() {
        this.FireballRotate()
    }
    // 注册武器旋转攻击定时器
    FireballRotate(){
        this.Player = find("Canvas/me")
        // console.log(this.node.eulerAngles.z)
        this.damage = this.Player.getComponent(PlayerController).FireAttack
        this.rotateSpeed = this.Player.getComponent(PlayerController).FireRotateSpeed
        
        // 第一把武器的旋转角度
        const firstAngles = this.node.parent.children[0].eulerAngles.z%360
        for(var i = 0;i<this.node.parent.children.length;i++){// 重新设置所有武器的角度，保证是大致对称位置
            this.node.parent.children[i].eulerAngles = new Vec3(0, 0, firstAngles+(360/this.node.parent.children.length)*i)
        }
        // 使每个武器保持固定距离
        // this.rotate = this.node.eulerAngles.z + this.rotate
        this.rotate = this.node.eulerAngles.z
        // console.log(this.node.eulerAngles.z,'zzzzzzz')
        // 设置定时器前先清除，避免多次添加
        clearInterval(this.FireballRunTimer)
        this.FireballRunTimer = setInterval(() => {
            this.node.eulerAngles = new Vec3(0, 0, this.rotate)
            if(this.rotate == 360){
                this.rotate = -3.6
            }
            this.rotate += 3.6
        }, this.rotateSpeed);
        // 获取碰撞组件
        this.collider = this.getComponent(CircleCollider2D);
        if (this.collider) {
            // 注册碰撞回调
            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
        this.startAttack()
        
    }

    // 开始攻击
    startAttack() {
        this.isAttacking = true;
        if (this.collider) {
            this.collider.enabled = true; // 启用碰撞检测
        }
    }

    // 结束攻击
    endAttack() {
        this.isAttacking = false;
        if (this.collider) {
            this.collider.enabled = false; // 禁用碰撞检测
        }
    }

    // 碰撞开始回调
    private onBeginContact(selfCollider: CircleCollider2D, otherCollider: CircleCollider2D, contact: IPhysics2DContact | null) {
        if (!this.isAttacking) return;
        // console.log("发生碰撞，碰撞对象组为:",otherCollider.group)
        // 检查碰撞对象是否是敌人
        if (otherCollider.group === 4) { // 敌人的碰撞分组为 4
            const enemy = otherCollider.node.getComponent('Enemy');
            if (enemy) {
                this.onHitEnemy(enemy);
            }
        }
        if (otherCollider.group === 16) { // 敌人的碰撞分组为 16
            const boss = otherCollider.node.getComponent('boss');
            if (boss) {
                this.onHitEnemy(boss);
            }
        }
    }

    // 击中敌人处理
    private onHitEnemy(enemy: any) {
        this.damage = this.Player.getComponent(PlayerController).FireAttack
        
        // console.log('击中敌人!');
        enemy.takeDamage(this.damage,this.node,'magic',this.ex);
    }
}